/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jfs.beans;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.*;
import javax.persistence.EntityManager;
import javax.persistence.Query;


/**
 *
 * @author Radek
 */
class KontoDAO {
        public boolean insertData(Konto p){
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("RezerwiorPU");
            
            List <Konto> l;
            boolean registered = false;
            EntityManager em= emf.createEntityManager();
            try{
                Query q = em.createNamedQuery("Konto.findAll");
                l = q.getResultList();
                for(Konto el:l){
                    if (el.getEmail().equals(p.getEmail()))
                        registered=true;
                }   
            } catch(Exception e) {
                e.printStackTrace();
            }
            if(!registered)
            {
                em.getTransaction().begin();
                em.persist(p);
                em.getTransaction().commit();
                em.close();
                return true;
            }else
            {
                em.close();
                return false;
            }
            }
    
    }


